package cn.tedu.charging.order.rabbitmq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class AmqpProducer<T> {
    private final RabbitTemplate rabbitTemplate;
    @Autowired
    public AmqpProducer(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    public boolean send(T message, String destination, String routingKey){
        try{
            rabbitTemplate.convertAndSend(destination, routingKey, message);
            return true;
        }catch (Exception e){
            log.error("发送消息失败,交换机:{},路由key:{},消息体:{}", destination, routingKey, message, e);
        }
        return false;
    }
    public boolean send(T message, String destination) {
        return send(message, destination, "");
    }
}
